package com.xbongbong.rule.symbol;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.rule.symbol.base.AbstractSymbol;
import com.xbongbong.saas.constant.StringConstant;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 等于判断(单个值相等或数组中某一值相等)
 *
 * @author zheng.li
 **/
public class EqualSymbol extends AbstractSymbol {

    @Override
    public boolean operation() {

        if (Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            //标签
            return Objects.equals(value.toString(), conditionValueStr);
        } else if (Objects.equals(fieldType, FieldTypeEnum.ADDRESS.getType())) {
            //地址
            if (attr.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                JSONArray addressArray = factJson.getJSONArray(attr);
                return addressArray.stream().anyMatch(address -> compareAddress((JSONObject) address));
            }
            return compareAddress(factJson.getJSONObject(attr));
        } else if (attr.contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
            //子表单
            JSONArray arrayValue = factJson.getJSONArray(attr);
            if (CollectionsUtil.isNotEmpty(arrayValue)) {
                if (Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                        || Objects.equals(fieldType, FieldTypeEnum.CHECKBOX_GROUP.getType())
                        || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                        || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                        || Objects.equals(fieldType, FieldTypeEnum.LINK_DATA_MULTI.getType())
                ) {
                    for (Object o : arrayValue) {
                        JSONArray array = (JSONArray) o;
                        if (CollectionsUtil.isNotEmpty(array) && array.stream().allMatch(v -> Objects.equals(v, conditionValue))) {
                            return true;
                        }
                    }
                }
                return arrayValue.stream().anyMatch(v -> Objects.equals(v, conditionValue));
            }
        } else if (Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.CHECKBOX_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.OWNERID.getType())
                || Objects.equals(fieldType, FieldTypeEnum.COUSERID.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LINK_DATA_MULTI.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LINK_BUSINESS_MULTI.getType())
        ) {
            //复选/下拉复选/成员多选/部门多选/负责人/协同人/关联数据多选/关联业务多选
            JSONArray arrayValue = factJson.getJSONArray(attr);
            if (CollectionsUtil.isNotEmpty(arrayValue)) {
                return arrayValue.stream().allMatch(v -> Objects.equals(v, conditionValue));
            }
        } else {
            //其他
            return Objects.equals(value, conditionValue);
        }
        return false;
    }

    private boolean compareAddress(JSONObject address2) {
        String conditionAddr = conditionValueList.stream().map(Object::toString).collect(Collectors.joining());
        String addrStr = address2.getString(PaasConstant.PROVINCE) + address2.getString(PaasConstant.CITY) + address2.getString(PaasConstant.DISTRICT);
        return addrStr.contains(conditionAddr);
    }
}
